package com.org.service.impl.current;

import com.org.core.exception.LogicException;
import com.org.dao.current.CurrentRoleMapper;
import com.org.dao.current.CurrentUserMapper;
import com.org.entity.current.CurrentRole;
import com.org.entity.current.CurrentUser;
import com.org.service.current.CurrentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CurrentUserServiceImpl implements CurrentUserService {

    @Autowired
    CurrentUserMapper currentUserMapper;

    @Autowired
    CurrentRoleMapper currentRoleMapper;

    /**
     * 获取当前登录用户的信息
     *
     * @param loginCode 登录账号
     * @return
     */
    @Override
    public CurrentUser getCurrentUser(String loginCode) {
        CurrentUser currentUser = currentUserMapper.getCurrentUser(loginCode);
        if (currentUser != null) {
            if (currentUser.getState() == 0){
                throw LogicException.of("该用户已经被禁用，请联系管理员！");
            }
            String fkUserId = currentUser.getUserId();
            List<CurrentRole> currentRoleList = currentRoleMapper.getCurrentRoleList(fkUserId);
            currentUser.setRoleList(currentRoleList);
        }
        return currentUser;
    }
}
